package com.fastCms.business.frame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.bim.pojo.po.ChangeStatusPO;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.daos.NewsMapper;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.entity.News;
import com.fastCms.business.frame.pojo.po.IdStrPO;
import com.fastCms.business.frame.pojo.po.NewsQueryPO;
import com.fastCms.business.frame.pojo.po.NewsSavePO;
import com.fastCms.business.frame.pojo.vo.NewsVO;
import com.fastCms.business.frame.service.NewsService;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 新闻资讯 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-05-23
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Resource
    private NewsMapper newsMapper;

    @Override
    public PageInfo<NewsVO> showPage(PageParam<NewsQueryPO> param) {
        NewsQueryPO newsQueryPO = param.getQueryFields();
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<NewsVO> newsList = newsMapper.showList(newsQueryPO);
        if (CollUtil.isNotEmpty(newsList)) {
            List<NewsVO> resultList = BeanConvertUtil.convertList(newsList, NewsVO.class);
            PageInfo<NewsVO> result = new PageInfo<>(resultList);
            return result;
        }
        return new PageInfo<>(Lists.newArrayList());
    }

    @Override
    public Boolean saveOrUpdate(NewsSavePO param) throws BusinessException {
        if (StringUtils.isBlank(param.getId())) {
            News news = new News();
            BeanUtil.copyProperties(news, param);
            this.save(news);
        } else {
            News news = getById(param.getId());
            if (news == null) {
                throw new BusinessException("数据不存在");
            }
            BeanUtil.copyProperties(news, param);
            this.updateById(news);
        }
        return true;
    }

    @Override
    public Boolean delete(IdStrPO param) throws BusinessException {
        News news = getById(param.getId());
        if (news == null) {
            throw new BusinessException("数据不存在");
        }
        news.setState(CommonConst.STATE.INVALID);
        this.updateById(news);
        return true;
    }

    @Override
    public Boolean changeStatus(ChangeStatusPO param) throws BusinessException {
        News news = getById(param.getId());
        if (news == null) {
            throw new BusinessException("数据不存在");
        }
        news.setStatus(param.getStatus());
        this.updateById(news);
        return true;
    }
}
